package com.example.service;

import cn.hutool.json.JSONArray;
import com.example.dao.ProblemCaseDao;
import com.example.dao.ProblemDao;
import com.example.model.dto.DoProblemLeft;
import com.example.model.vo.ProblemTable;
import com.example.model.pojo.Problem;
import com.example.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description //TODO
 * @Date 2022/9/13 19:46
 * @Author lonng
 **/
@Service
public class ProblemServiceImpl implements ProblemService{
    @Autowired
    private ProblemDao problemDao;
    @Autowired
    private TagService tagService;

    @Autowired
    private ProblemTagService problemTagService;

    @Autowired
    private ProblemCaseDao problemCaseDao;


    @Autowired
    private JsonUtil jsonUtil;
    @Override
    public String findAll() {
        Integer integer = 0;
        List<ProblemTable> problemTables=new ArrayList<>();
        for(Problem p:problemDao.findAll()){
            ProblemTable problemTable=new ProblemTable();
            problemTable.setId(p.getProblemId());
            problemTable.setTitle(p.getTitle());
            problemTable.setAnswer(p.getAnswer());
            problemTable.setPassrate(p.getPassrate());
            problemTable.setFrequency(p.getFrequency());
            problemTable.setDifficulty(p.getDifficulty());
            System.out.println(problemTable);
            problemTables.add(problemTable);
            integer++;
        }
        return "{\n" +
                "  \"code\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"count\": "+integer+",\n" +
                "  \"data\": "+jsonUtil.getJson(problemTables)+"}";
    }

    @Override
    public String findAllByTid(String tName) {
        Integer integer = 0;
        List<ProblemTable> problemTablesByDifficulty=new ArrayList<>();
        for(Problem p:problemDao.findAllById(problemTagService.findAllByTid(tagService.findAllByName(tName).getId()))){
            ProblemTable problemTableByTid=new ProblemTable();
            problemTableByTid.setId(p.getProblemId());
            problemTableByTid.setTitle(p.getTitle());
            problemTableByTid.setAnswer(p.getAnswer());
            problemTableByTid.setPassrate(p.getPassrate());
            problemTableByTid.setFrequency(p.getFrequency());
            problemTableByTid.setDifficulty(p.getDifficulty());
            System.out.println(problemTableByTid);
            problemTablesByDifficulty.add(problemTableByTid);
            integer++;
        }
        return "{\n" +
                "  \"code\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"count\": "+integer+",\n" +
                "  \"data\": "+jsonUtil.getJson(problemTablesByDifficulty)+"}";
    }

    @Override
    public DoProblemLeft getById(Integer id) {
        System.out.println(id);
        List<String> tNames=new ArrayList<>();
        for(Integer i:problemTagService.findAllByPid(id)){
            tNames.add(tagService.findByIdGetName(i));
        }
        System.out.println(tNames);

        DoProblemLeft doProblemLeft=new DoProblemLeft(id,problemDao.findByPId(id).getTitle(),
                tNames,problemDao.findByPId(id).getDifficulty(),problemDao.findByPId(id).getTimeLimit()
        ,problemDao.findByPId(id).getMemoryLimit(),problemDao.findByPId(id).getStakLmit(),problemDao.findByPId(id).getDescription(),
                problemDao.findByPId(id).getExamples(),problemDao.findByPId(id).getHint(),problemDao.findByPId(id).getGood());

        return doProblemLeft;
    }

    @Override
    public String findAllDifficulty(Integer difficulty) {
        Integer integer = 0;
        List<ProblemTable> problemTablesByDifficulty=new ArrayList<>();
        for(Problem p:problemDao.findAllByDifficulty(difficulty)){
            ProblemTable problemTableByDifficulty=new ProblemTable();
            problemTableByDifficulty.setId(p.getProblemId());
            problemTableByDifficulty.setTitle(p.getTitle());
            problemTableByDifficulty.setAnswer(p.getAnswer());
            problemTableByDifficulty.setPassrate(p.getPassrate());
            problemTableByDifficulty.setFrequency(p.getFrequency());
            problemTableByDifficulty.setDifficulty(p.getDifficulty());
            System.out.println(problemTableByDifficulty);
            problemTablesByDifficulty.add(problemTableByDifficulty);
            integer++;
        }
        return "{\n" +
                "  \"code\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"count\": "+integer+",\n" +
                "  \"data\": "+jsonUtil.getJson(problemTablesByDifficulty)+"}";
    }

    @Override
    public String findAllByTitleContains(String Title) {
        Integer integer = 0;
        List<ProblemTable> problemTablesByTitleContains=new ArrayList<>();
        for (Problem p:problemDao.findAllByTitleContains(Title)){
            ProblemTable problemTableFindByTitleContains=new ProblemTable();
            problemTableFindByTitleContains.setId(p.getProblemId());
            problemTableFindByTitleContains.setTitle(p.getTitle());
            problemTableFindByTitleContains.setAnswer(p.getAnswer());
            problemTableFindByTitleContains.setPassrate(p.getPassrate());
            problemTableFindByTitleContains.setFrequency(p.getFrequency());
            problemTableFindByTitleContains.setDifficulty(p.getDifficulty());
            System.out.println(problemTableFindByTitleContains);
            problemTablesByTitleContains.add(problemTableFindByTitleContains);
            integer++;
        }
        return "{\n" +
                "  \"code\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"count\": "+integer+",\n" +
                "  \"data\": "+jsonUtil.getJson(problemTablesByTitleContains)+"}";
    }

    @Override
    public Integer getCount() {
        Integer integer=0;
        for(Problem p:problemDao.findAll()){

            integer++;
        }
        return integer;
    }

    public Map<String, Object> getMap(String result, String requestBody, Integer Version , Integer pid, String language, String uid) {
        JSONArray jsonArray2 = null;
        jsonArray2 = new JSONArray(result);
        String errorMessage=jsonArray2.getJSONObject(0).get("files").toString();
        System.out.println(jsonArray2.getJSONObject(0).get("files"));
        Integer time= Integer.valueOf(jsonArray2.getJSONObject(0).get("runTime").toString());
        Integer memory= Integer.valueOf(jsonArray2.getJSONObject(0).get("memory").toString());
        Integer length=requestBody.trim().length();
        Integer version=Version+1;
        Map<String,Object> map=new HashMap<>();
        map.put("pid",pid);
        map.put("uid",uid);
        map.put("errorMessage",errorMessage);
        map.put("time",time);
        map.put("memory",memory);
        map.put("length",length);
        map.put("code",requestBody);
        map.put("language",language);
        map.put("version",version);
        map.put("gmtCreate",new Date());
        map.put("gmtModified",new Date());
        return  map;
    }


}
